package com.tacc.tyun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tacc.tyun.base.BaseServiceImpl;
import com.tacc.tyun.common.exception.TYException;
import com.tacc.tyun.dao.master.SysOssDao;
import com.tacc.tyun.common.jedis.RedisService;
import com.tacc.tyun.model.MailMsg;
import com.tacc.tyun.model.SysOss;
import com.tacc.tyun.service.MailMsgRepository;
import com.tacc.tyun.service.SysOssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author tao cong cong
 * @create 17/7/2 下午2:58
 */
@Service("sysOssService")
public class SysOssServiceImpl extends BaseServiceImpl<SysOss> implements SysOssService {

    @Resource
    private SysOssDao sysOssDao;

    @Autowired
    private RedisService redisService;
    @Autowired
    private MailMsgRepository repository;

    @Autowired
    private void setSysOssDao(SysOssDao sysOssDao){
        this.baseDao=sysOssDao;
    }

    @Override
    public PageInfo<SysOss> pageInfo(SysOss sysOss, int pageSize, int pageNum)  {
        PageHelper.startPage(pageNum,pageSize);
        Example example=new Example(SysOss.class);
        Example.Criteria criteria=example.createCriteria();
        //like这种条件
        List<SysOss> list=sysOssDao.selectByExample(example);

        //mongodb数据库
       /* MailMsg record=new MailMsg();
        record.setId("19");
        record.setSendDt(new Date());
        record.setSubject("18");
        record.setText("18");
        record.setTo("tyun");
        repository.save(record);*/

        MailMsg ma = repository.findByTo("1");
        System.out.print(ma);

        //数量
        System.out.print(repository.count());
        //按条件查询数量

        //repository.count(example1);


        //page页码，rows页面大小
        PageRequest pageRequest=new PageRequest(1,2);
        Page<MailMsg> page = repository.findAll(pageRequest);

        List<MailMsg> content = page.getContent();
        for (MailMsg mailMsg:content){
            System.out.print(mailMsg);
        }



        //redis缓存的使用
        try {
            redisService.set("yun","love");
        }catch (Exception e){
            throw new TYException("redis缓存失败！",e);
        }finally {
            //抛出后仍然处理
            return new PageInfo<SysOss>(list);
        }


    }

    @Override
    public int insertSysOssList(List<SysOss> sysOssList) {
        return sysOssDao.insertSysOssList(sysOssList);
    }
}
